
import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaTableNew } from 'comsMobx';
import RoomPlanDialog from '../public/roomPlan/index';
import {toJS} from 'mobx';
import { WeaErrorPage, WeaTools } from 'ecCom';
import MeetingDialog from '../public/dialog/index';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('meetingDialog')
@observer
class DialogSingle extends React.Component {
  constructor(props) {
    super(props);
    const queryNow = this.props.location.query;
    this.state = {
      type: queryNow.type || 'preview',
      RoomPlanContainer: false,
      meetingid: queryNow.meetingid || '',
      visible: true,
      //emobile端会议分享
      isfromchatshare: queryNow.isfromchatshare || 0,
      sharer:queryNow.sharer || 0,
      isDiscuss:queryNow.isDiscuss || 0,

      //新建的参数处理(以下参数没有需要的可以不传)
      isInterval: queryNow.isInterval || false, //是否周期会议
      begintime: queryNow.begintime || '', //开始日期
      endtime: queryNow.endtime || '', //结束日期
      address: queryNow.address || '', //会议室id
    }
  }

  componentWillReceiveProps(nextProps) {
    const {type, meetingid} = this.props.location.query;
    const nextType = nextProps.location.query.type;
    const nextId = nextProps.location.query.meetingid;
    if(nextType && nextType!== type){
      this.setState({type: nextType});
    }
    if(nextId && nextId !== meetingid){
      this.setState({meetingid: nextId});
    }
  }

  callParent = () => {
    window.opener.eventRegister.start('childRequest', {request: 'wo shi parent'}, (data)=> {
      // 父级方法执行成功的回调
      // 关闭当前页面
      window.close()
    },(error)=> {
      // 父级方法执行失败的回调
    })
  }

  render() {
    let { RoomPlanContainer, type, meetingid, visible, isInterval, isfromchatshare, sharer, isDiscuss } = this.state;
    const {from} = this.props.location.query;
    let urlParams = this.props.location.query;
    urlParams = toJS(urlParams);
    let isBtnDown = urlParams&&(urlParams.isBtnDown==='true') ? true : false;
    return (
      <div style={{height: '100%'}} onClick = {()=> from && this.callParent()}>
        {isBtnDown ? 
          <MeetingDialog ecId={`${this && this.props && this.props.ecId || ''}_MeetingDialog@r2foft`}
            setRoomPlan={this.setRoomPlan}
            setDialog={this.setDialog}
            setType={this.setType}
            setMeetingId={this.setMeetingId}
            setAll={this.setAll}
            type={type}//新建为'create',查看为'preview',编辑edit,会议决议---待定
            visible={visible}//显隐受控
            doRefesh={this.doRefesh}//自定义刷新页面，不会关掉弹出框
            meetingId={meetingid} //会议id，查看会议时用到
            isInterval={isInterval}//新建会议类型，true是周期会议， false是普通会议
            meetingDate={''}//新建日期
            isSingle={true}
            isBtnDown={true}
            isfromchatshare={isfromchatshare}
            sharer={sharer}
            urlParams={urlParams}
            isDiscuss = {isDiscuss}
          />
        : 
          <MeetingDialog ecId={`${this && this.props && this.props.ecId || ''}_MeetingDialog@9buoti`}
            setRoomPlan={this.setRoomPlan}
            setDialog={this.setDialog}
            setType={this.setType}
            setMeetingId={this.setMeetingId}
            setAll={this.setAll}
            type={type}//新建为'create',查看为'preview',编辑edit,会议决议---待定
            visible={visible}//显隐受控
            doRefesh={this.doRefesh}//自定义刷新页面，不会关掉弹出框
            meetingId={meetingid} //会议id，查看会议时用到
            isInterval={isInterval}//新建会议类型，true是周期会议， false是普通会议
            meetingDate={''}//新建日期
            isSingle={true}
            isfromchatshare={isfromchatshare}
            sharer={sharer}
            urlParams={urlParams}
            isDiscuss = {isDiscuss}
          />}
        {RoomPlanContainer && <RoomPlanDialog ecId={`${this && this.props && this.props.ecId || ''}_RoomPlanDialog@r9p3ts`}
          setRoomPlan={this.setRoomPlan}
          setDialog={this.setDialog}
          setType={this.setType}
          setMeetingId={this.setMeetingId}
          canCreate={false}
          height={600}
          visible={RoomPlanContainer} />}
      </div>
    )
  }
  
  doRefesh = () => {}
  setAll = (meetingid, bool, type)=>{
    this.setState({
      meetingid:meetingid,
      visible:bool,
      type:type
    })
  }
  setMeetingId = (meetingid) => {
    this.setState({ meetingid: meetingid })
  }
  setRoomPlan = (bool) => {
    this.setState({ RoomPlanContainer: bool })
  }
  setDialog = (bool) => {
    this.setState({ visible: bool })
  }
  setType = (type) => {
    this.setState({ type: type })
  }
}

//组件检错机制
class MyErrorHandler extends React.Component {
  render() {
    const hasErrorMsg = this.props.error && this.props.error !== "";
    return (
      <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@5b9w4d`} msg={hasErrorMsg ? this.props.error : getLabel(383324,"对不起，该页面异常，请联系管理员")} />
    );
  }
}

DialogSingle = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(DialogSingle);

export default DialogSingle
